package com.chinatelecom.check.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinatelecom.check.model.CheckTableQuestionOptionModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hutool.core.bean.BeanUtil;
import com.chinatelecom.framework.model.PageData;
import com.chinatelecom.framework.model.PageParam;
import com.chinatelecom.check.entity.CheckTableQuestion;
import com.chinatelecom.check.model.CheckTableQuestionModel;
import com.chinatelecom.check.dao.CheckTableQuestionMapper;
import com.chinatelecom.check.service.CheckTableQuestionService;
import com.chinatelecom.framework.generic.GenericServiceImpl;

/**
 * CheckTableQuestion服务实现
 */
@Service
@Transactional
public class CheckTableQuestionServiceImpl extends GenericServiceImpl<CheckTableQuestion> implements CheckTableQuestionService{
	
	@Autowired
	private CheckTableQuestionMapper mapper;
	
	@Override
	public CheckTableQuestionModel getDetail(Integer id) {
		CheckTableQuestionModel model = new CheckTableQuestionModel();
		CheckTableQuestion record = mapper.selectById(id);
		BeanUtil.copyProperties(record, model);
		return model;
	}
	
	@Override
	public PageData<CheckTableQuestionModel> page(PageParam param) {
		PageData<CheckTableQuestion> data = super.getPageData(param);

		List<CheckTableQuestionModel> list = data.getList().stream().map(a -> {
			CheckTableQuestionModel dto = new CheckTableQuestionModel();
			BeanUtils.copyProperties(a, dto);
			return dto;
		}).collect(Collectors.toList());

		PageData<CheckTableQuestionModel> result = new PageData<CheckTableQuestionModel>();
		BeanUtils.copyProperties(data, result);
		result.setList(list);
		return result;
	}

	@Override
	public List<CheckTableQuestionModel> getList(Integer tableId) {
		QueryWrapper<CheckTableQuestion> q = new QueryWrapper<>();
		q.eq("TABLE_ID", tableId);
		q.orderByAsc("sort");
		List<CheckTableQuestion> simpleList = super.getList(q);

		List<CheckTableQuestionModel> complexList = new ArrayList<>();
		for(CheckTableQuestion a: simpleList){
			CheckTableQuestionModel b = new CheckTableQuestionModel();
			BeanUtil.copyProperties(a, b);
			//处理图片
			if(tableId<4)
				b.setQuestionUrl("/static/"+b.getQuestion());
			//处理选项
			List<CheckTableQuestionOptionModel> optionList = JSON.parseArray(b.getOptionsJson(), CheckTableQuestionOptionModel.class);
			b.setOptionList(optionList);
			complexList.add(b);
		}
		return complexList;
	}


}
